package DatabaseManager;
import java.sql.*;

public class PDUMSGBox {

	private Connection con;
	private PreparedStatement preparedStmt;
	private ResultSet resultSet;
	
	private String url;
	private String id;
	private String pw;
		
	/**
	 * Methods
	 */
	public PDUMSGBox(){
		con = null;
		preparedStmt = null;
		resultSet = null;
		url = "jdbc:mysql://127.0.0.1:3306/PDUMSGBox"; //jdbc:mysql://url:port/'DBname'
		id = "root";
		pw = "wlqwndrydbr";
	}
	
	/**
	 * Connect DB
	 */
	public void ConnectDB(){
		
		try {
			Class.forName("com.mysql.jdbc.Driver"); 
			con = DriverManager.getConnection(url,id,pw);
			System.out.println("DB �좎룞�쇿뜝�숈삕 �뺝뜝�숈삕");
			//conn.close(); // �좎룞�쇿뜝�숈삕�좎룞���좎룞�쇿뜝�숈삕 �좎뙣�뚮벝��
		}
		catch (ClassNotFoundException cnte) {
			System.out.println(cnte.getMessage() );
		}
		catch (SQLException se) {
			System.out.println(se.getMessage());
		}
	}
	
	public void CloseConnectionDB() throws SQLException{
		con.close();
	}
	/**
	 * Store PDU
	 * @param pdu
	 */
	public void StorePduMessage(String msgID, String transactionID, String target){
		String sql = null;
		
		try{
			sql = "insert into PDUMSGBox values (?,?,?)";
			preparedStmt = con.prepareStatement(sql);
			preparedStmt.setString(1, msgID);	//MSGID
			preparedStmt.setString(2, transactionID);		//TransactionID
			preparedStmt.setString(3, target)
			preparedStmt.executeUpdate();
			System.out.println("pdumsgbox insert complete");
			
		}catch (SQLException se){
			System.out.println(se.getMessage());
		}
	}
	
	/**
	 * Delete PDU
	 * @param pdu
	 */
	
	public void DeletePduMessage(String msgID, String transactionID){
		String sql = null;
		
		try{
			sql = "delete from PDUMSGBox where MSGID = ? and TransactionID = ?";
			preparedStmt = con.prepareStatement(sql);
			preparedStmt.setString(1, msgID);
			preparedStmt.setString(2, transactionID);
			preparedStmt.executeUpdate();
			System.out.println("�좎룞�쇿뜝�숈삕�좎떦琉꾩삕");
		}catch(SQLException se){
			System.out.println(se.getMessage());
		}
	}
	
	/**
	 * �좎뙇�먯삕�좎룞���좎룞�쇿뜝�숈삕
	 * @param pdu
	 * @return
	 * @throws SQLException
	 */

	public boolean IsStoredPdu(String msgID, String transactionID) throws SQLException{
		String sql = null;
		try{
			sql = "select * from PDUMSGBox where MSGID = ? and TransactionID = ?";
			preparedStmt = con.prepareStatement(sql);
			preparedStmt.setString(1, msgID);
			preparedStmt.setString(2, transactionID);
			resultSet = preparedStmt.executeQuery();
		}catch(SQLException se){
			System.out.println(se.getMessage());
		}
		
		if(resultSet.next())
		{
			return true;
		}else
			return false;
	}



	public boolean IsExistTid(String mmsTransactionId) throws SQLException {
		String sql = null;
		try{
			sql = "select * from PDUMSGBox where TransactionID = ?";
			preparedStmt = con.prepareStatement(sql);
			preparedStmt.setString(1, mmsTransactionId);
			resultSet = preparedStmt.executeQuery();
		}catch(SQLException se){
			 System.out.println("Isexist error");
		}
		if(resultSet.next())
		{
			 System.out.println("jonajae");
			return true;
		}else
			 System.out.println("anjonjae");
			return false;
	}
	
	
	public String GetMessageID(String transactionId) throws SQLException
	{
		String sql = null;
		String result;
		try{
			sql = "select MSGID from PDUMSGBox where TransactionID = ?";
			preparedStmt = con.prepareStatement(sql);
			preparedStmt.setString(1, transactionId);
			resultSet = preparedStmt.executeQuery();
		}catch(SQLException se){
			
		}
		result = resultSet.getString("MSGID");
		
		return result;		
	}
	
	public String GetTransactionID(String msgId, String target) throws SQLException
	{
		String sql = null;
		String result;
	
		sql = "select TransactionID from PDUMSGBox where MSGID = ? and Target = ?";
		preparedStmt = con.prepareStatement(sql);
		preparedStmt.setString(1, msgId);
		preparedStmt.setString(2, target)
		resultSet = preparedStmt.executeQuery();
		
		result = resultSet.getString("TransactionID");
		
		return result;
	}
	
	
	
	
	
	
	
	

}
